define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'unilive/media/index' + location.search,
                    add_url: 'unilive/media/add',
                    edit_url: '',
                    del_url: 'unilive/media/del',
                    multi_url: 'unilive/media/multi',
                    table: 'unilive_media',
                }
            });

            $.extend(Table.api.formatter,{
                path: function (value, row, index) {
                    let image = '';
                    if (row.type == 'image') {
                        image = value ? value : '/assets/img/blank.gif';
                        var classname = typeof this.classname !== 'undefined' ? this.classname : 'img-sm img-center';
                        image = '<a href="javascript:"><img class="' + classname + '" src="' + Fast.api.cdnurl(value) + '" /></a>';
                    }
                    value = value === null ? '' : value.toString();
                    return '<div class="input-group input-group-sm" style="width:250px;margin:0 auto;">' +
                        '<input type="text" class="form-control input-sm" value="' + value + '">' +
                        '<span class="input-group-btn input-group-sm">' + image + '</span>' +
                        '</div>';
                },
                media: function (value, row, index) {
                    value = value === null ? '' : value.toString();
                    return '<div class="input-group input-group-sm" style="width:250px;margin:0 auto;">' +
                        '<input type="text" class="form-control input-sm" value="' + value + '">' +
                        '<span class="input-group-btn input-group-sm">' + '</span>' +
                        '</div>';
                },
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'type', title: __('Type'), searchList: {"image":__('Type image'),"voice":__('Type voice'),"video":__('Type video'),"thumb":__('Type thumb')}, formatter: Table.api.formatter.normal},
                        {field: 'path', title: __('Path'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.path},
                        {field: 'media_id', title: __('Media_id'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.media},
                        {field: 'expired', title: __('Expired'), searchList: {'0':__('No'), '1':__('Yes')}, operate:false,formatter: Table.api.formatter.status},
                        {field: 'passtime', title: __('Passtime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
            table.off('dbl-click-row.bs.table');
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
